<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function mergeSort(array) {
        if (array.length > 1) {
          const { length } = array;
          const middle = Math.floor(length / 2);
          const left = mergeSort(array.slice(0, middle));
          const right = mergeSort(array.slice(middle, length));
          array = merge(left, right);
        }
        return array;
      }

      function merge(left, right) {
        let i = 0;
        let j = 0;
        const result = [];
        while (i < left.length && j < right.length) {
          result.push(left[i] < right[j] ? left[i++] : right[j++]);
          // console.log(result);
          //先push ，再++
        }
        return result.concat(i < left.length ? left.slice(i) : right.slice(j));
      }
      console.log( mergeSort([1, 3, 43, 2, 34, 56]));
     
    </script>
  </body>
</html>
